package com.ashapo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

/**
 * 学生
 */
public class Student {

    private String id;

    private int age;

    private String name;

    private final ArrayList<Course> courses;

    /**
     * @param id   ID
     * @param age  年龄
     * @param name 姓名
     */
    public Student(String id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
        this.courses = new ArrayList<>();
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void selectCourse(Course course) {
        this.courses.add(course);
    }

    public boolean hasSelectedCourse(Course course) {
        return this.courses.contains(course);
    }

    /**
     * 返回课程下标
     *
     * @param course 课程
     * @return int
     */
    public int courseIndex(Course course) {
        if (this.courses.contains(course)) {
            return this.courses.indexOf(course);
        } else {
            return -1;
        }
    }

    /**
     * 对所选课程进行排序
     */
    public void sortCourse() {
        System.out.println("----------1排序前----------");
        this.courses.forEach((course) -> {
            System.out.println("课程时长:" + course.getCtime() + ",课程名:" + course.getName());
        });
        Collections.sort(this.courses);
        System.out.println("----------1排序后----------");
        this.courses.forEach((course) -> {
            System.out.println("课程时长:" + course.getCtime() + ",课程名:" + course.getName());
        });
        System.out.println("----------2排序前----------");
        this.courses.forEach((course) -> {
            System.out.println("课程时长:" + course.getCtime() + ",课程名:" + course.getName());
        });
        //以ctime进行顺序排序
        Collections.sort(courses, (Course o1, Course o2) -> {
            return o1.getCtime() - o2.getCtime();
        });
        System.out.println("----------2排序后----------");
        this.courses.forEach((course) -> {
            System.out.println("课程时长:" + course.getCtime() + ",课程名:" + course.getName());
        });
        System.out.println("----------3排序前----------");
        this.courses.forEach((course) -> {
            System.out.println("课程时长:" + course.getCtime() + ",课程名:" + course.getName());
        });
        //以ctime进行倒序排序
        courses.sort((Course o1, Course o2) -> {
            return o2.getCtime() - o1.getCtime();
        });
        System.out.println("----------3排序后----------");
        this.courses.forEach((course) -> {
            System.out.println("课程时长:" + course.getCtime() + ",课程名:" + course.getName());
        });
    }

    /**
     * 覆盖equals，判断两对象是否相同
     *
     * @param obj 对象
     * @return bool
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (obj instanceof Student) {
            Student stobj = (Student) obj;
            if (this.id.equals(stobj.id) && this.age == stobj.age && this.name.equals(stobj.name)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + Objects.hashCode(this.id);
        hash = 67 * hash + this.age;
        hash = 67 * hash + Objects.hashCode(this.name);
        return hash;
    }


}